package com.huida.app.java8.unit01;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author wangzhuangzhuang
 * @DESC:
 * @date 2022-02-04 16:23
 */
public class Demo03 {
	public static void main(String[] args) {
		List<String> names = Arrays.asList("Mal", "Wash", "Kaylee", "Inara",
				"Zoe", "Jayne", "Simon", "River", "Shepherd Book");

		Optional<String> first = names.stream()
				.filter(name -> name.startsWith("C"))
				.findFirst();

		System.out.println(first); // 打印Optional.empty

		System.out.println(first.orElse("None")); // 打印字符串None

		// 即便找到指定的名称，仍然使用逗号分割集合
		System.out.println(first.orElse(String.format("No result found in %s", names.stream().collect(Collectors.joining(", ")))));

		// 仅当Optional为空时，才使用逗号分割集合
		System.out.println(first.orElseGet(() -> String.format("No result found in %s", names.stream().collect(Collectors.joining(", ")))));

		List<String> nameListWith5Len = names.stream().filter(n -> n.length() == 5)
				.collect(Collectors.toList());
		System.out.println(nameListWith5Len); // [Inara, Jayne, Simon, River]
	}
}

//Optional.empty
//None
//No result found in Mal, Wash, Kaylee, Inara, Zoe, Jayne, Simon, River, Shepherd Book
//No result found in Mal, Wash, Kaylee, Inara, Zoe, Jayne, Simon, River, Shepherd Book